package xkp.lesson.store.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;

import java.util.List;

import xkp.lesson.store.Config;
import xkp.lesson.store.R;
import xkp.lesson.store.entity.Building;

public class BuildAdapter extends RecyclerView.Adapter<BuildAdapter.ViewHolder> {
    private List<Building> dataList;
    private LayoutInflater layoutInflater;
    private Context context;

    public BuildAdapter(Context context, List<Building> dataList) {
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(context);
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = layoutInflater.inflate(R.layout.build_item, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Building building = dataList.get(position);
        Log.i("url",Config.BASE_URL+building.getImgUrl());
        Picasso.with(context).load(Config.BASE_URL+building.getImgUrl()).into(holder.buildImg);
        //Glide.with(context).load(Config.BASE_URL+building.getImgUrl()).into(holder.buildImg);
        holder.deviceName.setText("设备名称："+building.getDeviceName());
        holder.deviceLocation.setText("设备位置："+building.getDeviceLocation());
        holder.description.setText("描述："+building.getDescription());
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView buildImg;
        private TextView deviceName;
        private TextView deviceLocation;
        private TextView description;



        ViewHolder(View itemView) {
            super(itemView);

            buildImg = itemView.findViewById(R.id.buildImg);

            deviceName = itemView.findViewById(R.id.deviceName);
            deviceLocation = itemView.findViewById(R.id.deviceLocation);
            description = itemView.findViewById(R.id.description);

        }
    }

    public void resresh(List<Building> dataList){
        this.dataList = dataList;
        notifyDataSetChanged();

    }
}
